//
//  ScrollHeader.m
//  网易Header
//
//  Created by 何欣 on 15/7/1.
//  Copyright (c) 2015年 何欣. All rights reserved.
//

#import "HomeScrollHeader.h"
#define ButtonWidth  60

@interface HomeScrollHeader ()<UIScrollViewDelegate>

@property (nonatomic,retain) UIScrollView *scrollView;
@property (nonatomic,assign) NSInteger currentButtonTag;

@end

@implementation HomeScrollHeader


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
       //初始化scrollView
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        _scrollView.delegate = self;
//        _scrollView.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:_scrollView];
        //标记当前按钮的Tag值
        _currentButtonTag = 1000;
    }
    return self;
}

//根据传入的数组 设置scrollView的偏移量 创建按钮
- (void)setScrollHeaderButtonTitleArray:(NSMutableArray *)array{

    _scrollView.contentSize = CGSizeMake(array.count * ButtonWidth, 40);
    for (int i = 0; i < array.count; i++) {
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(i * ButtonWidth, 0, ButtonWidth, 40)];
        button.tag = _currentButtonTag + i;
        [button setTitle:[array[i] tname]  forState:UIControlStateNormal];
        if (i == 0) {
            [self setButtonSelectStyle:button];
        }else{
            [self setButtonNomalStyle:button];
        }
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_scrollView addSubview:button];
    }
}


//按钮响应事件
-(void)buttonAction:(UIButton *)button{
    
    [self changeState:button];
    //代理方法
    [self.delegate clickButtonAtIndex:_currentButtonTag - 1000];
    
}

//滚动到某一位置
-(void)setScrollToIndex:(NSInteger)integer{
    
    UIButton *button = (UIButton *)[self viewWithTag:integer + 1000];
    [self changeState:button];
}

//改变按钮样式及滚动scrollView

-(void)changeState:(UIButton *)button{
    
    //根据currentButtonTag 找到点击前处于选中状态的按钮
    UIButton *previousButton = (UIButton *)[self viewWithTag:_currentButtonTag];
    //设置按钮样式
    [UIView animateWithDuration:0.3 animations:^{
        [self setButtonNomalStyle:previousButton];
        [self setButtonSelectStyle:button];
    }];
    
    //滚动scrollView
    
    [_scrollView scrollRectToVisible:CGRectMake((button.tag -1003) * ButtonWidth, 0, [UIScreen mainScreen].bounds.size.width, 40) animated:YES];
    
    //改变标记的按钮
    _currentButtonTag = button.tag;
}


//处于选中状态按钮的样式
-(void)setButtonSelectStyle:(UIButton *)button{

    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:20.0f];
}

//非选中状态样式
-(void)setButtonNomalStyle:(UIButton *)button{
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:17.0f];
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
